package IOtest;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
class MyCallable01 implements Callable<String>{
    @Override
    public String call() throws Exception {
        while(true){
            String str=Thread.currentThread().getName()+":call()方法正在运行";
            System.out.println(str);
            Thread.sleep(1000);
            return str;
        }
    }
}
public class ThreadTest2 {
    public static void main(String[] args)throws Exception {
        //创建Callable线程任务对象
        Callable<String> call1 = new MyCallable01();
        Callable<String> call2 = new MyCallable01();
        //通过FutureTask对象封装Callable对象
        FutureTask<String> f1 = new FutureTask<>(call1);
        FutureTask<String> f2 = new FutureTask<>(call2);
        //创建线程对象
        Thread t1 =new Thread(f1,"线程1");
        Thread t2 =new Thread(f2,"线程2");
        //启动线程
        t1.start();
        t2.start();
        while(true){
            if(f1.isDone()){
                System.out.println(f1.get());
            }
            if(f2.isDone()){
                System.out.println(f2.get());
            }
            System.out.println("main()");
            Thread.sleep(1000);
        }
    }
}
